package camid.main.jmeter;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class EnviromentVariableHandler 
{
	public static final String USER_PROPERTY = "camid.ssh.user";
	
	public static final String KEY_PROPERTY = "camid.ssh.key";
	
	public static final String MAIN_NODE_PROPERTY = "camid.main.node";
	
	public static final String WORK_NODES_PROPERTY = "camid.work.nodes";
	
	public static final String RUN_SCRIPT_PROPERTY = "camid.scripts.startnode";
	
	public static final String STOP_SCRIPT_PROPERTY = "camid.scripts.stopnode";
	
	public static final String NODE_PORT_PROPERTY = "camid.port";
	
	private static EnviromentVariableHandler INSTANCE;
	
	public synchronized static EnviromentVariableHandler getInstance()
	{
		if(INSTANCE == null)
		{
			INSTANCE = new EnviromentVariableHandler();
		}
		
		return INSTANCE;
	}
	
	private EnviromentVariableHandler()
	{
		parseEnvironmentVariables();
	}
	
	private String user;
	
	private File key;
	
	private String mainNode;
	
	private Integer port;
	
	private List<String> workNodes;
	
	private File startnodeScript;
	
	private File stopnodeScript;
	
	public void parseEnvironmentVariables()
	{
		String userProp = System.getProperty(USER_PROPERTY);
		String keyProp = System.getProperty(KEY_PROPERTY);
		
		System.out.println("User: " + userProp + "; Key: " + keyProp);
		
		if(checkIfNullOrBlank(userProp))
		{
			this.user = System.getProperty("user.name");
		}
		else
		{
			this.user = userProp;
		}
		
		if(keyProp != null)
		{
			File key = new File(keyProp);
			if(key.exists())
			{
				this.key = key;
			}
		}
		
		this.mainNode = System.getProperty(MAIN_NODE_PROPERTY);
		
		parseWorkNodes();
		
		this.port = stringToInteger(System.getProperty(NODE_PORT_PROPERTY));
		
		
		String startNodeScriptPath = System.getProperty(RUN_SCRIPT_PROPERTY);
		if(startNodeScriptPath != null)
		{
			startnodeScript = new File(startNodeScriptPath);
		}
		
		String stopNodeScriptPath = System.getProperty(STOP_SCRIPT_PROPERTY);
		if(stopNodeScriptPath != null)
		{
			stopnodeScript = new File(stopNodeScriptPath);
		}
	}
	
	
	
	private void parseWorkNodes() {
		String workNodesString = System.getProperty(WORK_NODES_PROPERTY);
		
		if(!checkIfNullOrBlank(workNodesString))
		{
			String[] workNodes = workNodesString.split("\\x3a");
			this.workNodes = Arrays.asList(workNodes);
		}
		
		
	}
	
	private static Integer stringToInteger(String str)
	{
		Integer result = null;
		try
		{
			result = Integer.parseInt(str);
		}
		catch(NumberFormatException e)
		{
			
		}
		
		return result;
	}

	private static boolean checkIfNullOrBlank(String str)
	{
		return str == null || str.trim().equals(" ") || str.trim().equals("");
	}
	
	
	public String getUser() {
		return user;
	}

	public File getKey() {
		return key;
	}

	public String getMainNode() {
		return mainNode;
	}

	public List<String> getWorkNodes() {
		return workNodes;
	}

	public void setWorkNodes(List<String> workNodes) {
		this.workNodes = workNodes;
	}

	public Integer getPort() {
		
		return this.port;
	}

	public File getStartnodeScript() {
		return startnodeScript;
	}

	public File getStopnodeScript() {
		return stopnodeScript;
	}
	
	
	
	
}
